<template>
  <div class="chart-box flex">
    <span class="none-text">暂无数据</span>
  </div>
</template>

<script>
import echarts from "echarts";
import resize from "./mixins/resize";
export default {
  mixins: [resize],
  props: {
    chartId: {
      type: String,
      default: "chart",
    },
    pieList: {
      type: Array,
      default: function () {
        return [];
      },
    },
    pieName: {
      type: String,
      default: "统计",
    },
  },
  data() {
    return {
      chart: null,
      name: "统计",
    };
  },
  watch: {
    pieList: {
      handler(val) {
        this.$nextTick(() => {
          // this.initChart(val);
        });
      },
      immediate: true,
      deep: true,
    },
  },

  methods: {
    initChart(data) {
      let list = [];
      if (data && data.length > 0) {
        list = data.map((el) => {
          const label = el.label ? el.label : " ";
          return {
            name: label,
            value: el.rate,
          };
        });
      }

      this.chart = echarts.init(document.getElementById(this.chartId));
      const option = {
        color: ["#3BA1FF", "#FBD438", "#4FCB74"],
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b}: {c} ({d}%)",
        },
        legend: {
          bottom: "5%",
          left: "center",
          textStyle: {
            // 图例文字的样式
            color: "#222",
            fontSize: 12,
          },
        },
        series: [
          {
            name: this.pieName,
            type: "pie",
            // radius: "50%",
            radius: ["30%", "50%"],
            data: list,
            emphasis: {
              itemStyle: {
                shadowBlur: 5,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };
      this.chart.setOption(option);
    },
  },
};
</script>

<style lang="scss" scoped>
.chart-box {
  width: 100%;
  min-height: 200px;
  .none-text {
    color: #909399;
    font-size: 14px;
  }
}
</style>
